/*
 *  Copyright 2020-2025 the original author or authors.
 *  You cannot use this file unless authorized by the author.
 */

package org.ipig.search.es.impl;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.ipig.search.conf.ElasticConf;
import org.ipig.search.es.ElasticService;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

/**
 * client管理
 *
 * @author <a href="mailto:comchnts@163.com">chinats</a>
 * @version $$Id$$
 * @since 1.0
 */
@Slf4j
public class ElasticManager implements ElasticService {
    /**
     * CLUSTER_NAME
     */
    public static final String CLUSTER_NAME = "cluster.name";
    /**
     * client 配置
     */
    @Setter
    private static ElasticConf conf;
    /**
     * TransportClient对象，用于连接ES集群
     */
    private volatile TransportClient client;
    /**
     * 构建Settings对象
     */
    private static Settings settings = null;
    /**
     * PreBuiltTransportClient
     */
    private PreBuiltTransportClient preBuiltTransportClient;

    /**
     * @return Client
     */
    public Client getClient() {
        if (client == null) {
            synchronized (ElasticManager.class) {
                try {
                    if (settings == null) {
                        settings = Settings.builder().put(CLUSTER_NAME, conf.getClusterName()).build();
                    }
                    preBuiltTransportClient = new PreBuiltTransportClient(settings);
                    String[] addressAry = conf.getAddress().split(",");
                    List<TransportAddress> list = new ArrayList<TransportAddress>();
                    for (int i = 0; i < addressAry.length; i++) {
                        String host = addressAry[i].split(":")[0];
                        int port = Integer.parseInt(addressAry[i].split(":")[1]);
                        TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(host), port);
                        list.add(transportAddress);
                    }
                    TransportAddress transportAddress[] = new TransportAddress[list.size()];
                    list.toArray(transportAddress);
                    client = preBuiltTransportClient.addTransportAddresses(transportAddress);
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
            }
        }
        return client;
    }

    /**
     *
     */
    public void close() {
        if (client != null) {
            try {
                client.close();
                log.info("客户端资源关闭成功!");
            } catch (Exception e) {
                log.warn("客户端资源关闭失败!", e);
            }
        }
    }

    /**
     * @return IndicesAdminClient
     */
    public IndicesAdminClient getAdminClient() {
        return getClient().admin().indices();
    }

}
